package com.ruoyi.kjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.AttendActivity;
import com.ruoyi.kjy.domain.EnterpriseActivity;
import com.ruoyi.kjy.service.AttendActivityService;
import com.ruoyi.kjy.service.EnterpriseActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 12/4,0004 19:52
 */

@RestController
@RequestMapping("/attendActivity")
public class AttendActivityController extends BaseController {


    @Autowired
    private AttendActivityService attendActivityService;

    @Autowired
    private EnterpriseActivityService enterpriseActivityService;

    @PostMapping("/attend")
    @Log(title = "参加活动", businessType = BusinessType.INSERT)
    public AjaxResult attend(@RequestBody AttendActivity attendActivity) {
        attendActivity.setStatus(0);
        attendActivity.setAttendId(SecurityUtils.getUserId());
        boolean save = attendActivityService.save(attendActivity);
        return toAjax(save);
    }

    @GetMapping
    public AjaxResult getInfo(Long current, Long size, @RequestParam(required = false) Long activityId, @RequestParam(required = false) Long attendId) {


        QueryWrapper<AttendActivity> wrapper = new QueryWrapper<>();


        if (Objects.isNull(current)) {
            current = 1L;
        }
        if (Objects.isNull(size)) {
            size = 10L;
        }
        if (size >= 20L) {
            size = 20L;
        }

        if (activityId!= null){
            wrapper.eq("activity_id", activityId);
        }
        if (attendId!= null){
            wrapper.eq("attend_id", attendId);
        }


        // 无所吊味
        Page<AttendActivity> page = attendActivityService.page(new Page<>(current, size), wrapper);

        for (AttendActivity record : page.getRecords()) {
            if (record.getActivityId()== null) continue;
            EnterpriseActivity byId = enterpriseActivityService.getById(record.getActivityId());
            if (byId!= null) {
            record.setName(byId.getName());
            }
        }

        return AjaxResult.success(page);
    }


    @Log(title = "参加活动", businessType = BusinessType.UPDATE)
    @PutMapping("")
    public AjaxResult edit(@RequestBody AttendActivity attendActivity) {
        return toAjax(attendActivityService.updateById(attendActivity));
    }


}
